package com.example.demoweb.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.example.demoweb.dao.AttachmentDao;
import com.example.demoweb.entity.Attachment;
import com.example.demoweb.framework.ExecuteResult;
import com.example.demoweb.framework.mybatis.common.ServiceException;
import com.example.demoweb.service.IAttachmentService;
import com.example.demoweb.util.FileupdownUtil;
/**
 * 对Attachment实体进行CRUD操作
 */
@Service("attachmentService")
@Transactional
public class AttachmentServiceImpl implements IAttachmentService{
	
	@Resource
	private AttachmentDao attachmentDao;
	
	@Autowired
	private FileupdownUtil fu;
	
	@Override
	@Transactional(readOnly = false)
	public void save(Attachment attachment) {
		attachmentDao.save(attachment);
	}
	
	@Override
	public void deleteById(String id) {
		attachmentDao.deleteById(id);
	}
	
	@Override
	public void update(Attachment attachment) {
		attachmentDao.update(attachment);
	}
	
	@Override
	public Attachment getById(String id) {
		return attachmentDao.getById(id);
	}
	
	@Override
	public Attachment getAttByRelaId(String relaId) {
		return attachmentDao.getAttByRelaId(relaId);
	}
	@Override
	public Attachment getByModel(Attachment attachment) {
		Attachment attachment1=attachmentDao.getByModel(attachment);
		return attachment1;
	}
	
	@Override
	public List<Attachment> searchList(Attachment attachment) {
		return attachmentDao.searchList(attachment);
	}
	
	@Override
	public List<Attachment> searchAttListByRelaId(String relaId) {
		return attachmentDao.searchAttListByRelaId(relaId);
	}

	@Override
	public void deleteByModel(Attachment attachment) {
		attachmentDao.deleteByModel(attachment);
	}
	
	@Override
	public void delete(Attachment paramT) throws ServiceException {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public List<Attachment> findAll(Attachment paramT) throws ServiceException {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public Attachment get(Attachment paramT) throws ServiceException {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public void insert(Attachment paramT) throws ServiceException {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public List<Attachment> list(Attachment paramT) throws ServiceException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	@Transactional(readOnly=false)
	public ExecuteResult<String> saveOne(MultipartFile file,String relaId) {
		ExecuteResult<String> result = new ExecuteResult<String>();
		Attachment attachment = new Attachment();
		attachment.setAttType("userhead");
		attachment = this.getByModel(attachment);
		if (null != attachment) {
			this.deleteById(attachment.getId());
		}
		Attachment saveAttachment=null;
		try {
			saveAttachment = fu.saveAttachment(file,relaId,"userhead", "personal");
			this.save(saveAttachment);
			result.setData("0");
			result.setSuccess(true);
		} catch (Exception e) {
			result.setData("1");
			result.setError("保存失败");
			result.setSuccess(false);
			throw new RuntimeException();
		}
		return result;
	}
}
